// mircoApp/new-auction/auction_bidding_details/auction_bidding_details.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: "0",
    list: [],
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.init();
    this.getData();
  },
  tapActive(e) {
    let {
      item
    } = e.currentTarget.dataset;
    if (item.has_one_activity.status == 2) return;
    wx.navigateTo({
      url: '/mircoApp/new-auction/new_auction_detail/new_auction_detail?id=' + item.activity_id,
    })
  },
  init() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      list: []
    })
  },
  async cancelAuction(e) {
    let {
      id
    } = e.detail;
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.new-auction.frontend.auction.cancelAuction"),
      data: {
        id: id
      }
    })
    if (data.result) {
      app.tips(data.msg)
      this.getData();
    } else {
      app.tips(data.msg)
    }
  },
  tabChange(e) {
    let {
      index
    } = e.detail;
    this.setData({
      active: index
    })
    this.init();
    this.getData();
  },
  async getData() {
    let urlstr = "plugin.new-auction.frontend.auction";
    if (this.data.active == "1") {
      urlstr = "plugin.new-auction.frontend.entrust";
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss(urlstr)
    })
    if (data.result) {
      this.setData({
        list: data.data.data,
        isLoadMore: true,
        total_page: data.data.last_page
      })
    } else {
      this.data.isLoadMore = false;
      app.tips(data.msg)
    }
  },
  async getMoreData() {
    if (!this.data.isLoadMore) return;
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let json = {
        page: this.data.page
      };
      let urlstr = "plugin.new-auction.frontend.auction";
      if (this.data.active == "1") {
        urlstr = "plugin.new-auction.frontend.entrust";
      }
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss(urlstr),
        data: json
      })
      this.data.isLoadMore = true;
      if (data.result) {
        this.setData({
          list: this.data.list.concat(data.data.data)
        })
      } else {
        this.setData({
          page: this.data.page - 1,
          isLoadMore: false
        })
        app.tips(data.msg)
      }
    }

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})